/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.sqlservice.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.RequiredArgsConstructor;
import me.zhengjie.mapper.WcsInboundMissionHistorySqlMapper;
import me.zhengjie.pojo.dto.WcsInboundMissionHistoryQueryCriteria;
import me.zhengjie.pojo.entity.NestingMiddleTable;
import me.zhengjie.pojo.entity.WcsInboundMissionHistory;
import me.zhengjie.pojo.vo.ReturnVo;
import me.zhengjie.sqlservice.WcsInboundMissionHistoryService;
import me.zhengjie.sqlservice.WcsInboundMissionHistorySqlService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import net.dreamlu.mica.core.utils.JsonUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author zhixiao
* @date 2025-06-28
**/
@Service
@RequiredArgsConstructor
public class WcsInboundMissionHistorySqlServiceImpl extends ServiceImpl<WcsInboundMissionHistorySqlMapper, WcsInboundMissionHistory> implements WcsInboundMissionHistorySqlService {

    private final WcsInboundMissionHistorySqlMapper wcsInboundMissionHistoryMapper;

    private final WcsInboundMissionHistoryService wcsInboundMissionHistoryService;

    @Override
    public PageResult<WcsInboundMissionHistory> queryAll(WcsInboundMissionHistoryQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(wcsInboundMissionHistoryMapper.findAll(criteria, page));
    }

    @Override
    public List<WcsInboundMissionHistory> queryAll(WcsInboundMissionHistoryQueryCriteria criteria){
        return wcsInboundMissionHistoryMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(WcsInboundMissionHistory resources) {
        wcsInboundMissionHistoryMapper.insert(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(WcsInboundMissionHistory resources) {
        WcsInboundMissionHistory wcsInboundMissionHistory = getById(resources.getId());
        wcsInboundMissionHistory.copy(resources);
        wcsInboundMissionHistoryMapper.updateById(wcsInboundMissionHistory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        wcsInboundMissionHistoryMapper.deleteBatchIds(ids);
    }

    @Override
    public void download(List<WcsInboundMissionHistory> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (WcsInboundMissionHistory wcsInboundMissionHistory : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("任务编码", wcsInboundMissionHistory.getInboundCode());
            map.put("任务状态", wcsInboundMissionHistory.getMissionStatus());
            map.put("容器编码", wcsInboundMissionHistory.getContainerCode());
            map.put("任务起点", wcsInboundMissionHistory.getSource());
            map.put("任务终点", wcsInboundMissionHistory.getDestination());
            map.put("创建时间", wcsInboundMissionHistory.getCreateTime());
            map.put("更新时间", wcsInboundMissionHistory.getUpdateTime());
            map.put("agvid", wcsInboundMissionHistory.getAgvId());
            map.put("容器类型", wcsInboundMissionHistory.getContainerType());
            map.put("入库类型", wcsInboundMissionHistory.getInboundType());
            map.put("优先级", wcsInboundMissionHistory.getPriority());
            map.put("任务类型", wcsInboundMissionHistory.getTaskType());
            map.put("是否回库嵌套（默认0,1是嵌套）", wcsInboundMissionHistory.getIsNest());
            map.put("0默认1plc", wcsInboundMissionHistory.getIsPlc());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public ReturnVo inboudHistoryMission(String inboundCode) {
        WcsInboundMissionHistory wcsInboundMissionHistory = wcsInboundMissionHistoryService.selectWcsinboundMissionHistory(inboundCode);
        if (wcsInboundMissionHistory != null){
            String nestingJson = wcsInboundMissionHistory.getNestingJson();
            List<NestingMiddleTable> list = JsonUtil.readValue(nestingJson,
                    new TypeReference<List<NestingMiddleTable>>() {});   // 关键：List 类型
            return ReturnVo.OK(list);
        }
        return ReturnVo.ng("未找到明细");
    }
}